#ifndef __KVSTORE_H__
#define __KVSTORE_H__
//#define LOG(_fmt, ...) fprintf(stdout, "[%s:%d]: "_fmt, __FILE__, __LINE__, ##__VA_ARGS__) // 如果没有参数（如示例 1），##__VA_ARGS__ 会自动消除多余的逗号，避免语法错误。

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "rbtree.h"
#include "hash.h"
#include "skiplist.h"
#include "content_node.h"
#include "nolock_list.h"
#include "master_slave.h"
#include "public.h"
#include "aof_persist.h"


#define BUFFER_LENGTH 1024



// ==== MASTER_SLACK ===========
typedef struct content
{
	char rbuffer[BUFFER_LENGTH];
	int rlen;
	char wbuffer[BUFFER_LENGTH];
	int wlen;
} content_t;



// rbtree
typedef RBTree rbtree_t;
typedef Node kvs_rbtree_node;


// hash
typedef HashTable hash_t;
typedef HashNode kvs_hash_node;


// skiptable
typedef SkipList skiplist_t;
typedef SNode kvs_skiplist_node;



// 协议解析
int kvs_request(content_node *rlist, content_node* wlist);
int kvs_split_token(char *msg, char *tokens[]);
int kvs_parse_protocol(content_node* wlist, char *tokens[], int count);




// 网络框架
int reactor_entry(int port);
int proactor_entry(int port);
int nty_entry(int port);

// 引擎存储+返回

//rbtree
int kvs_rbtree_create(rbtree_t **rbt);
int kvs_rbtree_destroy(rbtree_t *rbt);
int kvs_rbtree_set(rbtree_t *rbt, char *key, char *value);
char* kvs_rbtree_get(rbtree_t *rbt, char *key);
int kvs_rbtree_del(rbtree_t *rbt, char *key);
int kvs_rbtree_all_del(rbtree_t *rbt);
int kvs_rbtree_mod(rbtree_t *rbt, char *key, char *value);
int kvs_rbtree_exist(rbtree_t *rbt, char *key);
int kvs_rbtree_count(rbtree_t *rbt);


//hash
int kvs_hash_create(hash_t **ht, size_t bucket_count);
int kvs_hash_destroy(hash_t *ht);
int kvs_hash_set(hash_t *ht, char *key, char *value);
char* kvs_hash_get(hash_t *ht, char *key);
int kvs_hash_del(hash_t *ht, char *key);
int kvs_hash_all_del(hash_t *ht);
int kvs_hash_mod(hash_t *ht, char *key, char *value);
int kvs_hash_exist(hash_t *ht, char *key);
int kvs_hash_count(hash_t *ht);



//skiptable
int kvs_skiplist_create(skiplist_t **sl);
int kvs_skiplist_destroy(skiplist_t *sl);
int kvs_skiplist_set(skiplist_t *sl, char *key, char *value);
char* kvs_skiplist_get(skiplist_t *sl, char *key);
int kvs_skiplist_del(skiplist_t *sl, char *key);
int kvs_skiplist_all_del(skiplist_t *sl);
int kvs_skiplist_mod(skiplist_t *sl, char *key, char *value);
int kvs_skiplist_exist(skiplist_t *sl, char *key);
int kvs_skiplist_count(skiplist_t *sl);

#endif